<template>
  <div class="page-container">
    <div class="page-content">
      <div class="panel-container">
        <div class="panel-content">
          <div class="panel-left">
            <el-radio-group v-model="tabActive" style="margin-right: 30px" size="mini" @change="handleTabChange">
              <el-radio-button v-for="item in tabList" :key="item.value" :label="item.value">{{ item.label }}({{ item.num }})</el-radio-button>
            </el-radio-group>
            <el-button class="show-search" type="text" @click="handleShowSearch">
              <span v-if="showSearch"> 收起搜索 </span>
              <span v-else> 展开搜索 </span>
              <i class="el-icon-arrow-down" :class="{ rotate: showSearch }"></i>
            </el-button>
          </div>
          <!--搜索-->
          <SearchUiNew ref="SearchUiNew" v-show="showSearch" style="margin-top: 10px" :searchFormArr="searchFormArr" @searchParma="searchParma"></SearchUiNew>
        </div>
      </div>
      <!--数据信息-->
      <div class="panel-container no-margin">
        <el-table stripe :data="dataList" size="mini" :header-cell-style="{ background: '#F5F5F5', color: '#000000' }" ref="table" style="width: 100%" @selection-change="handleSelectionChange" v-loading="dataListLoading">
          <af-table-column type="selection" fixed="left" width="55"> </af-table-column>
          <af-table-column align="center" fixed="left" header-align="center" label="对比双方" prop="title" width="200">
            <template slot-scope="scope"> {{ scope.row.j_source }}和Feijiu </template>
          </af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="对方信息标题" prop="j_title" width="200">
            <template slot-scope="scope">
              <a v-if="scope.row.j_href" :href="scope.row.j_href" target="_blank">
                {{ scope.row.j_title }}
              </a>
              <span v-else>
                {{ scope.row.j_title }}
              </span>
            </template>
          </af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="发布时间" prop="j_fb_datetime" width="200"></af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="我方信息标题(相似)" prop="f_title" width="200">
            <template slot-scope="scope">
              <a v-if="scope.row.f_href" :href="scope.row.f_href" target="_blank">
                {{ scope.row.f_title }}
              </a>
              <span v-else>
                {{ scope.row.f_title }}
              </span>
            </template>
          </af-table-column>
          <af-table-column align="center" header-align="center" show-overflow-tooltip label="发布时间" prop="f_fb_datetime" width="200"></af-table-column>
          <af-table-column v-if="tabActive === 2" align="center" header-align="center" show-overflow-tooltip label="对比人" prop="operator" width="200"></af-table-column>
          <af-table-column v-if="tabActive === 2" align="center" header-align="center" show-overflow-tooltip label="对比时间" prop="compared_time" width="200"></af-table-column>
          <af-table-column v-if="tabActive === ''" align="center" header-align="center" show-overflow-tooltip label="状态" width="150">
            <template slot-scope="scope">
              {{ getSts(scope.row.sts) }}
            </template>
          </af-table-column>
          <af-table-column v-if="tabActive === '' || tabActive === 2 || tabActive === 3" align="center" header-align="center" show-overflow-tooltip label="对比结果" prop="results" width="100">
            <template slot-scope="scope">
              {{ getResult(scope.row.results) }}
            </template>
          </af-table-column>
          <af-table-column align="center" fixed="right" header-align="center" label="操作" width="250">
            <template slot-scope="scope">
              <el-button type="text" @click="handleContrast(scope)" v-if="scope.row.sts === 1">人工对比</el-button>
              <el-button type="text" @click="handleContrast(scope)" v-if="scope.row.sts === 2">已对比</el-button>
              <el-button type="text" @click="handleContrast(scope)" v-if="scope.row.sts === 3">人工修正</el-button>
            </template>
          </af-table-column>
        </el-table>
        <el-pagination
          small
          background
          :current-page="pageNumber"
          :page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="pageTotal"
          @current-change="currentChangeHandle"
          @size-change="sizeChangeHandle"
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 20px; text-align: right"
        ></el-pagination>
        <div class="panel-line" style="margin-top: 20px">
          <el-button v-if="tabActive === 1" type="primary" @click="handleContrastMultiple" size="mini" :disabled="multipleSelection.length === 0" :loading="loading">批量对比</el-button>
        </div>
      </div>
    </div>

    <!--  人工对比 -->
    <el-dialog :title="currentItem.sts === 3 ? '人工修正' : '人工对比'" :visible.sync="showDialogContrast" width="30%">
      <DialogContrast ref="DialogContrast" v-if="showDialogContrast" @close="showDialogContrast = false" :fId="currentItem.f_id" :sts="currentItem.sts" @submit="handleSubmit" @delete="handleDelete" />
    </el-dialog>
  </div>
</template>

<script>
import { apiToboCompared, apiAreaList, apiModifyInfo, apiModifyBatchInfo, apiDeleteInfo } from '@/api/biddingNotice'
import DialogContrast from './components/DialogContrast.vue'

export default {
  name: 'CompetitorManagement',
  components: { DialogContrast },
  data() {
    const searchList1 = ['both', 'area', 'time', 'result']
    const searchList2 = ['both', 'area', 'time']
    const searchList3 = ['both', 'area', 'operator', 'time', 'result']
    const paramsList1 = ['both', 'area', 'start_time', 'end_time', 'result']
    const paramsList2 = ['both', 'area', 'start_time', 'end_time']
    const paramsList3 = ['both', 'area', 'operator', 'start_time', 'end_time', 'result']
    const stsList = [
      { value: '', label: '全部', num_name: 'total', num: 0, searchList: searchList1, paramsList: paramsList1 },
      { value: 1, label: '待人工对比', num_name: 'w1', num: 0, searchList: searchList2, paramsList: paramsList2 },
      { value: 2, label: '已人工对比', num_name: 'w2', num: 0, searchList: searchList3, paramsList: paramsList3 },
      { value: 3, label: '自动对比', num_name: 'w3', num: 0, searchList: searchList1, paramsList: paramsList1 },
    ]
    const resultList = [
      { value: 1, label: '我方早发' },
      { value: 2, label: '其他' },
    ]
    return {
      stsList,
      resultList,
      multipleSelection: [],
      showSearch: false,
      tabActive: '',
      tabList: stsList,
      searchFormArr: [
        {
          fieldName: '对比双方:',
          type: 'checkboxGroup',
          field: 'both',
          hidden: false,
          placeholder: '请选择',
          default: [],
          option: [
            { value: 'hhs', label: 'hhs与Feijiu' },
            { value: 'fb', label: 'fb与Feijiu' },
            { value: 'wy', label: 'wy与Feijiu' },
          ],
        },
        {
          fieldName: '地区:',
          type: 'select',
          field: 'area',
          hidden: false,
          option: [],
        },
        {
          fieldName: '对比人:',
          type: 'input',
          field: 'operator',
          hidden: false,
          placeholder: '请输入对比人',
        },
        {
          fieldName: '发布时间:',
          type: 'timePicker',
          field: 'time',
          hidden: false,
          placeholder: '请选择发布时间',
          timeType: 'daterange',
          format: 'yyyy-MM-dd',
        },
        {
          fieldName: '对比结果:',
          type: 'checkboxGroup',
          field: 'result',
          hidden: false,
          placeholder: '请选择',
          default: [],
          option: resultList,
        },
      ],
      searchParams: {
        area: '',
        both: '',
        result: '',
        operator: '',
        start_time: '',
        end_time: '',
      },
      dataList: [],
      pageNumber: 1,
      pageSize: 10,
      pageTotal: 0,
      dataListLoading: false,
      showDialogContrast: false,
      currentItem: {},
      loading: false,
    }
  },
  activated() {
    this.$refs.table.doLayout()
  },
  mounted() {
    this.init()
  },
  destroyed() {},
  methods: {
    // 初始化
    init() {
      // 初始化筛选
      this.searchFormChange()
      this.getSelectList()
      this.getDataList()
    },
    handleShowSearch() {
      this.showSearch = !this.showSearch
    },
    searchFormChange() {
      const showList = this.tabList.find((item) => item.value === this.tabActive).searchList
      this.searchFormArr.forEach((item) => {
        if (showList.includes(item.field)) {
          item.hidden = false
        } else {
          item.hidden = true
        }
      })
    },
    // 获取下拉数据
    getSelectList() {
      apiAreaList().then((res) => {
        const formItem = this.searchFormArr.find((item) => item.field === 'area')
        formItem.option = res.info.map((item) => {
          return {
            value: item,
            label: item,
          }
        })
      })
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      let info = { pageNumber: this.pageNumber, pageSize: this.pageSize, condition: this.tabActive, label: '拍卖' }
      const paramsList = this.tabList.find((item) => item.value === this.tabActive).paramsList
      paramsList.forEach((item) => {
        info[item] = this.searchParams[item]
      })
      apiToboCompared(info)
        .then((res) => {
          const { data, total, others } = res
          this.dataList = data
          this.pageTotal = total
          this.dataListLoading = false
          this.tabList.forEach((item) => {
            item.num = others[item.num_name]
          })
          this.$refs.table.doLayout()
        })
        .catch((e) => {
          this.dataList = []
          this.pageTotal = 0
          this.dataListLoading = false
        })
    },
    // 列表查询
    searchParma(e) {
      let list = ['time']
      this.searchFormArr.forEach((item) => {
        if (list.includes(item.field)) return
        if (e[item.field]) {
          this.searchParams[item.field] = e[item.field]
        } else {
          this.searchParams[item.field] = ''
        }
      })

      if (e.time) {
        this.searchParams.start_time = e.time[0]
        this.searchParams.end_time = e.time[1]
      } else {
        this.searchParams.start_time = this.searchParams.end_time = ''
      }
      this.pageNumber = 1
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageNumber = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageNumber = val
      this.getDataList()
    },
    // 切换tab
    handleTabChange() {
      this.searchFormChange()
      this.pageNumber = 1
      this.getDataList()
    },
    // 获取状态
    getSts(value) {
      const item = this.stsList.find((item) => item.value === value)
      if (item) {
        return item.label
      } else {
        return ''
      }
    },
    // 获取对比结果
    getResult(value) {
      const item = this.resultList.find((item) => item.value === value)
      if (item) {
        return item.label
      } else {
        return ''
      }
    },
    // 人工对比
    handleContrast(scope) {
      this.currentItem = scope.row
      this.showDialogContrast = true
    },
    // 表格多选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 多选对比
    handleContrastMultiple() {
      const ids = this.multipleSelection.map((item) => item.id).join(',')
      this.loading = true
      apiModifyBatchInfo({ ids }).then(() => {
        this.loading = false
        this.$message({
          type: 'success',
          message: '操作成功',
        })
        this.multipleSelection.forEach((item) => {
          item.sts = 2
        })
        this.$refs.table.clearSelection()
      })
    },
    // 人工对比
    handleSubmit(f_id) {
      apiModifyInfo({
        id: this.currentItem.id,
        f_id,
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '操作成功',
          })
          this.currentItem.sts = 2
          this.showDialogContrast = false
          this.$refs.DialogContrast.handleDone()
        })
        .catch(() => {
          this.$refs.DialogContrast.handleDone()
        })
    },
    // 删除
    handleDelete() {
      apiDeleteInfo({
        id: this.currentItem.id,
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '操作成功',
          })
          this.currentItem.sts = 1
          this.showDialogContrast = false
          this.$refs.DialogContrast.handleDone()
        })
        .catch(() => {
          this.$refs.DialogContrast.handleDone()
        })
    },
  },
}
</script>
